package com.weishuang.monitor.leader.controller.api;

import cn.hutool.json.JSONUtil;
import com.weishuang.monitor.leader.commons.entity.Result;
import com.weishuang.monitor.leader.commons.enums.RCODE;
import com.weishuang.monitor.leader.commons.utils.ResultUtils;
import com.weishuang.monitor.leader.entity.*;
import com.weishuang.monitor.leader.service.*;
import com.weishuang.monitor.leader.entity.*;
import com.weishuang.monitor.leader.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/serversBaseInfo")
public class ServerBaseInfoAPI {

    @Autowired
    private IServersBaseInfoService serversBaseInfoService;

    @Autowired
    private IServerCpuRealTimeService serverCpuRealTimeService;

    @Autowired
    private IServerRamRealTimeService serverRamRealTimeService;

    @Autowired
    private IServerDiskInfoService serverDiskInfoService;

    @Autowired
    private IServerProcessInfoService serverProcessInfoService;

    @Autowired
    private IServersRunInfoService serversRunInfoService;

    /**
     * 接收服务器基本信息
     * @param sysId
     * @param serversBaseInfo
     * @return
     */
    @PostMapping("upBaseInfo/{sysId}")
    public Result onlineServer(@PathVariable String sysId, ServersBaseInfo serversBaseInfo) {
        serversBaseInfo.setId(sysId);
        serversBaseInfoService.onlineServer(serversBaseInfo);
        serversRunInfoService.onlineServer(sysId);
        return ResultUtils.success();
    }

    /**
     * 接收cpu运行信息
     * @param sysId
     * @param serverCpuRealTime
     * @return
     */
    @PostMapping("upCpuRealTime/{sysId}")
    public Result upCpuRealTime(@PathVariable String sysId, @Validated ServerCpuRealTime serverCpuRealTime) {
        serverCpuRealTime.setSysId(sysId);
        serverCpuRealTimeService.save(serverCpuRealTime);
        serversRunInfoService.upCpu(serverCpuRealTime);
        return ResultUtils.success();
    }

    /**
     * 接收内存运行信息
     * @param sysId
     * @param serverCpuRealTime
     * @return
     */
    @PostMapping("upRamRealTime/{sysId}")
    public Result upRamRealTime(@PathVariable String sysId, @Validated ServerRamRealTime serverCpuRealTime) {
        serverCpuRealTime.setSysId(sysId);
        serverRamRealTimeService.save(serverCpuRealTime);
        serversRunInfoService.upRam(serverCpuRealTime);
        return ResultUtils.success();
    }

    /**
     * 接受磁盘信息
     * @param sysId
     * @param diskInfos
     * @return
     */
    @PostMapping("upServerDiskInfo/{sysId}")
    public Result upServerDiskInfo(@PathVariable String sysId, String diskInfos) {
        if (StringUtils.isNotBlank(diskInfos)) {
            List<ServerDiskInfo> serverDiskInfos = JSONUtil.toList(diskInfos, ServerDiskInfo.class);
            if (serverDiskInfos != null && serverDiskInfos.size() > 0) {
                serverDiskInfos.forEach(serverDiskInfo -> {
                    serverDiskInfo.setSysId(sysId);
                });
            }
            //删除，新增
            serverDiskInfoService.upServerDiskInfo(sysId,serverDiskInfos);
            serversRunInfoService.upDisk(sysId,serverDiskInfos);
            return ResultUtils.success();
        }else{
            return ResultUtils.success(RCODE.OPERATION_SUCCESS_NOT_DATA);
        }
    }

    @PostMapping("upServerProcessInfos/{sysId}")
    public Result upServerProcessInfos(@PathVariable String sysId, String processInfos) {
        List<ServerProcessInfo> serverProcessInfos;
        if (processInfos != null && !"".equals(processInfos)) {
            serverProcessInfos = JSONUtil.toList(processInfos, ServerProcessInfo.class);
            serverProcessInfoService.upServerProcessInfos(sysId, serverProcessInfos);
            return ResultUtils.success();
        }else{
            return ResultUtils.success(RCODE.OPERATION_SUCCESS_NOT_DATA);
        }

    }

}
